package com.ln.lnzw.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ScreenUtils;
import com.bumptech.glide.Glide;
import com.example.library.banner.BannerLayout;
import com.ln.lnzw.R;
import com.ln.lnzw.bean.WcmBean;
import com.ln.lnzw.utils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import static android.content.ContentValues.TAG;

/**
 * Created by test on 2017/11/22.
 */


public class WebBannerAdapter extends RecyclerView.Adapter<WebBannerAdapter.MzViewHolder> {

    private Context context;
//    private List<WcmBean.ListDatasBean> list;
    private List<String> urlList;
    private BannerLayout.OnBannerItemClickListener onBannerItemClickListener;

    public WebBannerAdapter(Context context, List</*WcmBean.ListDatasBean*/String> urlList) {
        this.context = context;
        this.urlList = urlList;
//        urlList = new ArrayList<>();
//        for (int i=0;i<list.size();i++){
//            Log.i("123", "WebBannerAdapter: "+list.get(i).getCimgs().get(0));
//            urlList.add(list.get(i).getCimgs().get(0));
//        }
    }

    public void setOnBannerItemClickListener(BannerLayout.OnBannerItemClickListener onBannerItemClickListener) {
        this.onBannerItemClickListener = onBannerItemClickListener;
    }

    @Override
    public MzViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MzViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false));
    }

    @Override
    public void onBindViewHolder(MzViewHolder holder, final int position) {
        ImageView img = (ImageView) holder.imageView;
        CardView cardView = holder.cardView;
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) cardView.getLayoutParams();
        layoutParams.width= ScreenUtils.getScreenWidth()-250;
        layoutParams.height=ScreenUtils.getScreenHeight()/4-80;
        cardView.setLayoutParams(layoutParams);
//        ImageUtil.suitDisplayWideFill(img,2,1);


        if (urlList == null || urlList.isEmpty())
            return;
        final int P = position % urlList.size();
        String url = urlList.get(P)/*.getCimgs().get(0)*/;
        Log.i(TAG, "onBindViewHolder: "+url);
        if ("".equals(url)){
            Glide.with(context).load(url).centerCrop().placeholder(R.mipmap.icon_zanweitu).into(img);
        }else{
            Glide.with(context).load(url).into(img);
        }

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBannerItemClickListener != null) {
                    onBannerItemClickListener.onItemClick(P);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        if (urlList != null) {
           return urlList.size();
        }
       return 0;
    }


    class MzViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        CardView cardView;
        LinearLayout ll_layout;
         MzViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image);
             cardView = (CardView) itemView.findViewById(R.id.cardView);
             ll_layout = (LinearLayout) itemView.findViewById(R.id.ll_layout);

        }
    }

}
